(*
  Copyright (c) 2009 Barry Schwartz

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*)

module type Bits = Nibbletrie.Bits

module type S =
sig
  type t (* with sexp *)
  type elt
  type 'value nibbletrie
  val empty    : t
  val add      : elt -> t -> t
  val remove   : elt -> t -> t
  val mem      : elt -> t -> bool
  val of_elements : elt list -> t
  val elements : t -> elt list
  val iter     : (elt -> unit) -> t -> unit
  val to_nibbletrie : t -> unit nibbletrie
  val keyset   : 'value nibbletrie -> t 
end

module Make(Elt : Bits) :
  (S with type elt = Elt.t
     and type 'value nibbletrie = 'value Nibbletrie.Make(Elt).t) =
struct
  module Trie = Nibbletrie.Make(Elt)
  type t = unit Trie.t
  type elt = Elt.t
  type 'value nibbletrie = 'value Trie.t

  let empty  = Trie.empty
  let add e  = Trie.add e ()
  let remove = Trie.remove
  let mem    = Trie.mem

  let of_elements p =
    List.fold_left (fun set elem -> add elem set) empty p

  let elements set =
    let enlist_element e _v p =
      e :: p
    in
      Trie.fold enlist_element set []

  let iter f set =
    Trie.iter (fun k _v -> f k) set

  let keyset nt =
    Trie.map (fun _v -> ()) nt

  let to_nibbletrie set =
    set
end
